﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Server.CacheData;
using CPPEI.Coolzon.Service;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    class ReqUploadViewStateHandler : BaseHandler<ReqUploadViewStateData, RespUploadViewStateData>
    {
        public ReqUploadViewStateHandler()
            : base(SCENARIO.CONNECT, COMMAND.CONNECT.UPLOADVIEWSTATE)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        protected override RespUploadViewStateData OnHandle(Data.MainServiceData.Request request, ReqUploadViewStateData requestData)
        {
            RespUploadViewStateData responseData = new RespUploadViewStateData();
            responseData.IsNeesSyncViewState = false;
            responseData.ServerIp = CZEnvironment.Instance.ServerIP;
            responseData.ServerPort = CZEnvironment.Instance.ServerPort;
            if (requestData.HasViewState)
            {
                CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);
                CacheData.Client client = CacheData.ServerData.Instance.GetClient(request.Header.UserIdentity);


                if (domain != null
                    && client != null
                    && client.ControlState == CacheData.ControlState.UnderControl)
                {
                    if(client.Callback == null)
                    {
                        Common.Logger.InfoToTag("MainService", string.Format("重新设置client:{0}的callback", client.UserIdentity));
                        client.Callback = OperationContext.Current.GetCallbackChannel<IMainServiceCallback>();
                    }
                    if(client.Status != ClientStatus.Normal)
                    {
                        Common.Logger.InfoToTag("MainService", "client处于非Normal状态，不处理UploadViewState,Status:{0},State:{1},UserIdentity：{2}", client.Status, client.State,client.UserIdentity);

                        return null;
                    }
                    if (client.State == ClientState.EnteringDomain)
                    {
                        client.State = ClientState.InDomain;
                    }

                    #region client为操作员
                    if (domain.OperatorIdentity == client.UserIdentity)
                    {
                        domain.UpdateMainServer(requestData.MainServerIP, requestData.MainServerPort);

                        //client方案与服务器方案不同
                        if (!string.Equals(domain.SolutionName, requestData.ViewStateSolution))
                        {
                            Common.Logger.WarningToTag("MainService", "服务端的方案与操作员方案不同，更新成为终端:{0}发送的状态，方案:{1},Token:{2},Age:{3}", request.Header.UserIdentity, requestData.ViewStateSolution, requestData.ViewStateToken, requestData.ViewStateAge);
                            domain.UpdateViewState(requestData.ViewStateSolution, requestData.ViewStateAge, requestData.ViewStateToken);
                        }
                        else if(domain.ViewStateAge != requestData.ViewStateAge)
                        {
                            Common.Logger.WarningToTag("MainService", "服务端的方案与操作员方案相同Age不同，更新成为终端:{0}发送的状态，方案:{1},Token:{2},Age:{3}", request.Header.UserIdentity, requestData.ViewStateSolution, requestData.ViewStateToken, requestData.ViewStateAge);
                            domain.UpdateViewState(requestData.ViewStateSolution, requestData.ViewStateAge, requestData.ViewStateToken);
                        }
                    }
                    #endregion
                }
            }
            return responseData;
        }
    }
}